package com.yiwan.app.preventionsis.view;

import android.content.Context;
import android.graphics.*;
import android.os.AsyncTask;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import com.yiwan.app.preventionsis.R;
import com.yiwan.app.preventionsis.util.ScreenResolution;

public class FloatImageView extends View
{
    private static Context context;
    private FloatingTask floatingTask;
    private Paint paint;
    private float yPos = 0.0F;
    private float xPos = 0.0F;
    private final float XSPEED = 0.25F;
    private final float YSPEED = 0.1875F;
    private float xDistance;
    private float yDistance;
    private boolean directionX = true;
    private boolean directionY = true;
    private Bitmap bitmapRes;
    private float imgHeightInPix;

    public FloatImageView(Context paramContext, AttributeSet paramAttributeSet)
    {
        super(paramContext, paramAttributeSet);
        context = paramContext;
        releaseRes();
        this.paint = new Paint(1);
        this.floatingTask = new FloatingTask();
        if (Build.VERSION.SDK_INT >= 11)
        {
            this.floatingTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            return;
        }
        this.floatingTask.execute();
    }

    private void releaseRes()
    {
        if (this.bitmapRes != null)
        {
            this.bitmapRes.recycle();
            this.bitmapRes = null;
        }
    }

    private void floatImage(float xSpeed, float ySpeed)
    {
        if (this.directionX) {
            if (this.xPos + xSpeed * this.xDistance > this.xDistance) {
                this.xPos = (this.xDistance - (this.xPos + xSpeed * this.xDistance - this.xDistance));
                this.directionX = false;
            } else {
                this.xPos += xSpeed * this.xDistance;
            }
        } else {
            if(this.xPos < xSpeed * this.xDistance){
                this.xPos = (xSpeed * this.xDistance - this.xPos);
                this.directionX = true;
            } else {
                this.xPos -= xSpeed * this.xDistance;
            }
        }
        if (this.directionY){
            if (this.yPos + ySpeed * this.yDistance > this.yDistance) {
                this.yPos = (this.yDistance - (this.yPos + ySpeed * this.yDistance - this.yDistance));
                this.directionY = false;
            } else {
                this.yPos += ySpeed * this.yDistance;
            }
        } else {
            if(this.yPos < ySpeed * this.yDistance){
                this.yPos = (ySpeed * this.yDistance - this.yPos);
                this.directionY = true;
            } else {
                this.yPos -= ySpeed * this.yDistance;
            }
        }
        invalidate();
    }

    protected void onDetachedFromWindow()
    {
        if (this.floatingTask != null)
            this.floatingTask.stop();
        super.onDetachedFromWindow();
    }

    protected void onDraw(Canvas paramCanvas)
    {
        paramCanvas.save();
        paramCanvas.translate(this.xPos, this.yPos);
        paramCanvas.drawBitmap(this.bitmapRes,
                new Rect(0, 0, this.bitmapRes.getWidth(), this.bitmapRes.getHeight()),
                new Rect(0, 0, this.bitmapRes.getWidth(), this.bitmapRes.getHeight()),
                this.paint);
        paramCanvas.restore();
    }

    protected void onSizeChanged(int w, int y, int oldw, int oldy)
    {
        super.onSizeChanged(w, y, oldw, oldy);
        this.yPos = 0.0F;
        this.xPos = 0.0F;
        if (this.imgHeightInPix == 0.0F)
        {
            this.floatingTask.pause();
            releaseRes();
            float density;
            if(isInEditMode()) {
                density = 1.5f;
            } else {
                density = ScreenResolution.getDM().density;
            }
            this.imgHeightInPix = (density * 32.0F);
            Bitmap localBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.float_txt);
            float scaleRate = this.imgHeightInPix / localBitmap.getHeight();
            Matrix localMatrix = new Matrix();
            localMatrix.setScale(scaleRate, scaleRate);
            this.bitmapRes = Bitmap.createBitmap(localBitmap, 0, 0, localBitmap.getWidth(), localBitmap.getHeight(), localMatrix, true);
            this.xDistance = w - this.bitmapRes.getWidth();
            this.yDistance = y - this.bitmapRes.getHeight();
            this.floatingTask.run();
        }
    }

    public void restartFloat() {
        if (floatingTask.isCancelled()) {
            floatingTask = new FloatingTask();
            floatingTask.execute();
        }
    }

    public void cancelFloat() {
        if (floatingTask != null) {
            floatingTask.cancel(true);
        }
    }

    public void pauseFloat(){
        if (floatingTask != null && floatingTask.isRunning()) {
            floatingTask.pause();
        }
    }

    public void resumeFloat(){
        if (floatingTask != null) {
            if(floatingTask.isCancelled()){
                floatingTask = new FloatingTask();
                floatingTask.execute();
            } else if(!floatingTask.isRunning()){
                floatingTask.resume();
            }
        }
    }

    class FloatingTask extends AsyncTask<Integer, Integer, Void>
    {
        public boolean run = true;
        public boolean pause = true;
        private final int SLEEP_TIME = 15;

        @Override
        protected Void doInBackground(Integer[] paramArrayOfInteger)
        {
            while (this.run)
                if (!this.pause)
                {
                    this.publishProgress();
                    try
                    {
                        Thread.sleep(SLEEP_TIME);
                    }
                    catch (InterruptedException localInterruptedException)
                    {
                        onCancelled();
                        localInterruptedException.printStackTrace();
                    }
                }
            return null;
        }

        protected void run()
        {
            this.pause = false;
        }

        protected void pause()
        {
            this.pause = true;
        }

        protected void onProgressUpdate(Integer... values)
        {
            floatImage(XSPEED/100, YSPEED/100);
        }

        protected void stop()
        {
            this.run = false;
        }

        protected void onCancelled()
        {
            this.run = false;
            super.onCancelled();
        }

        protected void onPreExecute()
        {
            super.onPreExecute();
        }

        protected boolean isRunning(){
            return this.run;
        }

        protected void resume() {
            this.pause = false;
        }
    }
}